package cn.bdqn.asecu.service;

import cn.bdqn.asecu.utility.Utility_project;
import cn.bdqn.pojo.Operationlog;
import cn.bdqn.pojo.Project;
import cn.bdqn.pojo.User;
import cn.bdqn.repository.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


@Service
public class AsecuUser_Service {

    @Resource
    private Asecu_UserRepository asecu_userRepository;

    @Resource
    private Asecu_OperationLogRepository asecu_operationLogRepository;

    @Resource
    private Asecu_MyworkbenchRepository asecu_myworkbench;

    @Resource
    private Asecu_projectRepository1 asecu_projectRepository1;

    @Resource
    private Asecu_ThetypeRepository asecu_thetypeRepository;
    //用户查询
    public  User  Seluser(Integer id){
        Optional<User> byId = asecu_userRepository.findById(id);
        return byId.get();
    }
    //用户查询
    public  List<User>  x(){
        List<User> list = asecu_userRepository.findAll();
        return list;
    }
    //登录
    public User loing_verify(String name, String password){
        User byname = asecu_userRepository.findByname(name);
       if(byname!=null){
           if (byname.getPassword().equals(password)){
               return byname;
           }else{
               return null;
           }
       }
       return null;
    }
   //添加日志
    public void  addlog(Operationlog operationlog){
        asecu_operationLogRepository.save(operationlog);
    }
    //待办事项
    public List<Utility_project> backlogSel(Integer userid, Integer typeid, Integer roleid){

        Specification<Project> spec1=new Specification<Project>() {
            @Override
            public Predicate toPredicate(Root<Project> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                if (roleid!=2){
                    if (typeid!=0){
                    return cb.and(cb.equal(root.get("userId"),userid),cb.equal(root.get("agendaid"),typeid),cb.equal(root.get("mywrokbench_id"),0));
                    }
                    return cb.and(cb.equal(root.get("userId"),userid),cb.equal(root.get("mywrokbench_id"),0));
                }

                if (roleid==2){
                    if (typeid!=0){
                        return cb.and(cb.equal(root.get("agendaid"),typeid),cb.equal(root.get("mywrokbench_id"),0));
                    }
                    return cb.and(cb.equal(root.get("mywrokbench_id"),0));
                }

                return null;
            }
        };
        List<Project> all = asecu_projectRepository1.findAll(spec1);
        List<Utility_project> ulist=new ArrayList<>();
        for (Project p:all) {
            Utility_project utility_project=new Utility_project();
            utility_project.setProject(p);
            utility_project.setUser(Seluser(p.getUserId()));
            utility_project.setThetype(asecu_thetypeRepository.findById(p.getAgendaid()).get());
            ulist.add(utility_project);
        }

        return ulist;
    }

    public List<Utility_project> Selproject(){
        List<Project> all = asecu_projectRepository1.findAll();
        List<Utility_project> ulist=new ArrayList<>();
        for (Project p:all) {
            Utility_project utility_project=new Utility_project();
            utility_project.setProject(p);
            utility_project.setUser(Seluser(p.getUserId()));
            ulist.add(utility_project);
        }

        return ulist;
    }
    //待办处理
    public int disposeUpl(int id,String conductor){
        return asecu_projectRepository1.updatmywrokbench_id(1, id,conductor);
    }

    public List<Project> project_zt(){
        Specification<Project> spec=new Specification<Project>() {
            @Override
            public Predicate toPredicate(Root<Project> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                return cb.and(cb.equal(root.get("mywrokbench_id"),1));
            }
        };

        return asecu_projectRepository1.findAll(spec);
    }


}
